<?php

namespace app\index\controller;

use app\common\controller\IndexBaseController;
use think\facade\Db;

class Lists extends IndexBaseController
{
    protected function initialize()
    {
        parent::initialize();
    }


    public function index()
    {
        $param = $this->request->get();
        if(empty($param['catedir'])){
            return '404';
        }
        if(empty($param['areapy1'])){
            return '404';
        }

        $cate = Db::name('articles_cate')->where('dirname',$param['catedir'])->find();
        if(empty($cate)){
            return '404';
        }
        $this->assign('cate', $cate);
        $cates = getCatePath($cate);
        foreach($cates as $k => $v){
            $this->assign('cate'.($k+1), $v);
        }
        $mod = Db::name('articles_models')->find($cate['model_id']);
        $this->assign('mod', $mod);
        

        $area1 = Db::name('area')->where([
            'pinyin' => $param['areapy1'],
            'pid' => 0
        ])->find();
        $this->assign('area1', $area1);
        if(!empty($param['areapy2'])){
            $area2 = Db::name('area')->where([
                'pinyin' => $param['areapy2'],
                'pid' => $area1['id']
            ])->find();
            $this->assign('area2', $area2);
            
            if(!empty($param['areapy3'])){
                $area3 = Db::name('area')->where([
                    'pinyin' => $param['areapy3'],
                    'pid' => $area2['id']
                ])->find();
                $this->assign('area3', $area3);
            }
        }

        
        $where = [];
        $where[] = ['is_show','=',1];
        $ids = getChildCateIds($cate['id']);
        $ids .= $cate['id'];
        $where[] = ['cate_id','in',$ids];
        $where[] = ['areaid1','=',$area1['id']];
        if(!empty($area2)){
            $where[] = ['areaid2','=',$area2['id']];
        }
        if(!empty($area3)){
            $where[] = ['areaid3','=',$area3['id']];
        }
        
        if(empty($param['page']) || !is_numeric($param['page'])){
            $page = 1;
        }else{
            $page = $param['page'];
        }
        $limit = 20;
        $count = Db::name($mod['tablename'])->where($where)->count();
        $total = ceil($count / $limit);
        if($page > $total){
            $page = 1;
        }
        $index = $page - 1;
        $skip = $index * $limit;
        $lists = Db::name($mod['tablename'])->where($where)->order('id', 'desc')->limit($skip,$limit)->select()->toArray();
        foreach($lists as $k => $v){
            $lists[$k]['url'] = makeArcUrl($v['id'],$v['cate_id'],$v['areaid1'],$v['areaid2']);
            if(empty($v['litpic'])){
                $lists[$k]['litpic'] = '/default.jpg';
            }
        }
        $this->assign('lists', $lists);

        $url = '/'.$param['areapy1'].'_'.$param['areapy2'].'/'.$param['catedir'].'/';
        if(!empty($param['areapy3'])){
            $url .= $param['areapy3'].'/';
        }
        $pages = makePages($url,$page,$total,$count);
        $this->assign('pages', $pages);
        $pagesm = makePagesM($url,$page,$total,$count);
        $this->assign('pagesm', $pagesm);

        if($mod['id'] > 2){
            $templete = '/dianpu_list';
        }else{
            $templete = '/article_list';
        }
        return  $this->fetch($templete);

        
    }

    
    


}
